package cn.yuanqiao.archive.sealmanage;

import com.itextpdf.text.*;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class PdfUtil {

    /**
     * pdf插入图片
     *
     * @param
     * @param newPath 插入图片后的路径
     * @param
     * @throws IOException
     * @throws DocumentException
     */
    public static void addImage(InputStream inputStream, String newPath, byte[] waterMarkImage, int x, int y, float width, float height,boolean isALLAdd) throws IOException, DocumentException {
        //InputStream inputStream = oldPath;
        FileOutputStream out = new FileOutputStream(new File(newPath));
        PdfReader reader = new PdfReader(inputStream);
        //pdf页数
        int pdfPages = reader.getNumberOfPages();
        PdfStamper stamper = new PdfStamper(reader, out);
        //图片
        //BufferedImage bufferedImage = ImageIO.read(Files.newInputStream(Paths.get(imgPath)));

        //x轴坐标
        //int x = 350;
        //y轴坐标
        //int y = 390;
        //图片放置的页码
        PdfContentByte content;
        if(isALLAdd) pdfPages = 1;
        for (int i = 1; i <= pdfPages; i++) {
            //图片处理
            content = stamper.getOverContent(i);
            content.beginText();
            //Image img = Image.getInstance(ImageUtil.imageToBytes((bufferedImage), "png"));
            Image img = Image.getInstance(waterMarkImage);
            //设置图片大小
            content.saveState();
            img.scaleAbsolute(width, height);
            //img.setTransparency(new int[0]);
            //设置图片位置
            img.setAbsolutePosition(x, y);
            //PdfContentByte content = stamper.getOverContent(i);
            PdfGState gs = new PdfGState();
            //设置图片的透明度
            gs.setFillOpacity(1f);
            //gs.setFillOpacity(0.2f);

            content.setGState(gs);
            content.addImage(img);
            content.restoreState();
            content.endText();
        }
        //bufferedImage.flush();
        //inputStream.close();
        //关闭资源
        stamper.close();
        reader.close();
        out.close();
    }

   /* public static void main(String[] args) {
        try {
            System.out.println("=====添加归档章开始=====");
            InputStream inputStream = new FileInputStream(new File("E:\\exportlog\\testpdf.pdf"));
            ByteArrayOutputStream qRcodePngStream = getQRcodePngStream("E:\\exportlog\\1.png");
            byte[] data = qRcodePngStream.toByteArray();
            PdfUtil.addImage(inputStream,"E:\\exportlog\\testpdf.pdf", data,350,390,250,250);
            System.out.println("=====添加归档章结束=====");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }*/
    /*public static ByteArrayOutputStream getQRcodePngStream(String path) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        FileImageInputStream input = null;
        try {
            input = new FileImageInputStream(new File(path));
            byte[] buf = new byte[1024];
            int numBytesRead = 0;
            while ((numBytesRead = input.read(buf)) != -1) {
                output.write(buf, 0, numBytesRead);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                output.close();
                if(input!=null) input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return output;
    }*/
}
